#include<bits/stdc++.h>
using namespace std;

#define int long long 
#define mod 998244353
#define inf 0x3f3f3f3f
#define N 200010
#define V 1000010
#define ls(x) tr[x].l
#define rs(x) tr[x].r
int a[N],n,t;
int rt,cnt,v[V];
struct un{
    int l,r,laz,mx;
}tr[V<<2];
void push_up(int x){
    tr[x].mx=max(tr[ls(x)].mx,tr[rs(x)].mx);
    return;
}
void push_down(int x){
    if(tr[x].laz){
        tr[ls(x)].mx+=tr[x].laz;
        tr[rs(x)].mx+=tr[x].laz;
        tr[ls(x)].laz+=tr[x].laz;
        tr[rs(x)].laz+=tr[x].laz;
        tr[x].laz=0;
    }
    return;
}
void build(int &x,int l,int r){
    x=++cnt;
    tr[x]=un{0,0,0,0};
    if(l==r)return;
    int mid=(l+r)>>1;
    build(ls(x),l,mid);
    build(rs(x),mid+1,r);
    push_up(x);
    return;
}
void insert(int x,int l,int r,int p,int v){
    if(l==r){
        tr[x].mx=v;
        return;
    }
    push_down(x);
    int mid=(l+r)>>1;
    if(mid>=p)insert(ls(x),l,mid,p,v);
    else insert(rs(x),mid+1,r,p,v);
    push_up(x);
    return;
}
int query(int x,int l,int r,int p){
    if(l==r)return tr[x].mx;
    push_down(x);
    int mid=(l+r)>>1;
    if(mid>=p)return query(ls(x),l,mid,p);
    return query(rs(x),mid+1,r,p);
}
void clear(){
    rt=cnt=0;
    memset(v,0,sizeof(v));
    return;
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%lld",&t);
    while(t--){
        clear();
        scanf("%lld",&n);
        for(int i=1;i<=n;++i){
            scanf("%lld",&a[i]);
        }
        build(rt,1,1000000);
        for(int i=2;i<=n;++i){
            int y=query(rt,1,1000000,a[i])+a[i];
            if(!v[a[i]])y=tr[rt].mx;
            y=max(y,tr[rt].mx);
            if(a[i]==a[i-1])tr[rt].laz+=a[i];
            insert(rt,1,1000000,a[i-1],y);
            v[a[i-1]]=1;
        }
        printf("%lld\n",tr[rt].mx);
    }
    return 0;
}